/*
 * This file provided by Facebook is for non-commercial testing and evaluation
 * purposes only.  Facebook reserves all rights not expressly granted.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

package com.image.fresco.zoomable;

import android.content.Context;
import android.net.Uri;
import androidx.annotation.IdRes;
import androidx.annotation.LayoutRes;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.interfaces.DraweeController;
import com.lib.util.FileUtil;

import java.io.File;
import java.util.List;

public abstract class ZonmablePagerAdapter extends BaseZoomablePagerAdapter<String> {

    private Context mContext;
    private LayoutInflater mLayoutInflater;

    private boolean mAllowSwipingWhileZoomed = true;

    public ZonmablePagerAdapter(Context context, List<String> strings) {
        super(strings);
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }


    public void setAllowSwipingWhileZoomed(boolean allowSwipingWhileZoomed) {
        mAllowSwipingWhileZoomed = allowSwipingWhileZoomed;
    }

    public boolean allowsSwipingWhileZoomed() {
        return mAllowSwipingWhileZoomed;
    }

    public void toggleAllowSwipingWhileZoomed() {
        mAllowSwipingWhileZoomed = !mAllowSwipingWhileZoomed;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View page = mLayoutInflater.inflate(getPageResId(), null, false);
        ZoomableDraweeView zoomableDraweeView = (ZoomableDraweeView) page.findViewById(
                getZoomableDraweeViewResId());
        zoomableDraweeView.setAllowTouchInterceptionWhileZoomed(mAllowSwipingWhileZoomed);
        // needed for double tap to zoom
        zoomableDraweeView.setIsLongpressEnabled(true);
        zoomableDraweeView.setTapListener(new DoubleTapGestureListener(mContext,
                                                                       zoomableDraweeView));
        String url = convertUrl(position);
        Uri uri;
        if (FileUtil.isUrl(url)) {
            uri = Uri.parse(url);
        } else {
            uri = Uri.fromFile(new File(url));
        }
        DraweeController controller = Fresco.newDraweeControllerBuilder()
                                            .setUri(uri)
                                            .setAutoPlayAnimations(true)
                                            .build();
        zoomableDraweeView.setController(controller);
        //page.requestLayout();
        container.addView(page);
        return page;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        FrameLayout page = (FrameLayout) object;
        ZoomableDraweeView zoomableDraweeView = (ZoomableDraweeView) page.findViewById(
                getZoomableDraweeViewResId());
        zoomableDraweeView.setController(null);
        super.destroyItem(container, position, object);
    }

    @Override
    public int getItemPosition(Object object) {
        // We want to create a new view when we call notifyDataSetChanged() to have the correct behavior
        return POSITION_NONE;
    }

    protected abstract
    @LayoutRes
    int getPageResId();

    protected abstract
    @IdRes
    int getZoomableDraweeViewResId();

    /**
     * 七牛的gif地址可能需要转换
     */
    protected String convertUrl(int position) {
        return getItem(position);
    }

}
